package ua.pp.fland.multithreading.sorting;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Maxim Bondarenko
 * @version 1.1 Jul 4, 2010
 */

public class FileDataInput implements DataInputController {
    private static final Logger logger = Logger.getLogger(FileDataInput.class);

    private final String fileName;

    public FileDataInput(String fileName) {
        ArgumentGuard.checkNotNull(fileName, "fileName");
        ArgumentGuard.checkNotBlank(fileName, "fileName");
        this.fileName = fileName;
    }

    public List<Integer> getUnsortedData() throws IOException {
        List<Integer> res = new ArrayList<Integer>();

        List<String> lines = FileUtils.readLines(new File(fileName), "UTF-8");
        for (String line : lines) {
            try {
                res.add(Integer.parseInt(line));
            } catch (NumberFormatException e) {
                logger.error("Cannot cast input data to int. Ignoring that value . Error: " + e, e);
            }
        }
        return res;
    }
}
